from dataclasses import dataclass
from typing import Dict, List, Optional
import random

@dataclass
class Enemy:
    name: str
    max_hp: int
    current_hp: int
    attack: int
    defense: int
    abilities: List[Dict]
    level: int
    
    def __post_init__(self):
        """在 dataclass 的 __init__ 之后初始化其他属性"""
        self.next_action = None
        self.prepare_next_action()
    
    @classmethod
    def generate_random_enemy(cls, floor: int) -> 'Enemy':
        """根据楼层生成随机敌人"""
        enemy_types = {
            "史莱姆": {
                "base_hp": 15,
                "base_attack": 5,
                "base_defense": 2,
                "abilities": [
                    {"name": "分裂", "type": "ability", "damage": 3, "probability": 0.3},
                    {"name": "粘液攻击", "type": "attack", "damage": 4, "probability": 0.7}
                ]
            },
            "骷髅战士": {
                "base_hp": 20,
                "base_attack": 8,
                "base_defense": 3,
                "abilities": [
                    {"name": "骨矛突刺", "type": "attack", "damage": 6, "probability": 0.6},
                    {"name": "盾牌格挡", "type": "defense", "damage": 0, "probability": 0.4}
                ]
            }
        }
        
        # 随机选择敌人类型
        enemy_type = random.choice(list(enemy_types.keys()))
        base_stats = enemy_types[enemy_type]
        
        # 根据楼层增加难度
        level_multiplier = 1 + (floor - 1) * 0.2
        
        return cls(
            name=enemy_type,
            max_hp=int(base_stats["base_hp"] * level_multiplier),
            current_hp=int(base_stats["base_hp"] * level_multiplier),
            attack=int(base_stats["base_attack"] * level_multiplier),
            defense=int(base_stats["base_defense"] * level_multiplier),
            abilities=base_stats["abilities"],
            level=floor
        )
    
    def prepare_next_action(self):
        """准备下一个行动"""
        ability = random.choice(self.abilities)
        if random.random() <= ability["probability"]:
            self.next_action = {
                "type": "ability",
                "name": ability["name"],
                "damage": ability["damage"]
            }
        else:
            self.next_action = {
                "type": "attack",
                "name": "普通攻击",
                "damage": self.attack
            }
    
    def get_next_action(self) -> Dict:
        """获取下一个行动的信息"""
        return self.next_action
    
    def take_action(self) -> Dict:
        """执行行动"""
        action = self.next_action
        self.prepare_next_action()  # 准备下一次行动
        return action 